<?php 
namespace app\admin\controller;

use app\admin\model\MessageModel;
use app\admin\model\MemberModel;
use app\admin\model\UserModel;

/**
 * 留言板
 */
class Message extends Base
{
	
	public function index()
	{
		$message = new MessageModel();
		$member  = new MemberModel();
		$user    = new UserModel();

		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");
		$search   = input("param.search/d");
		$reply    = input("param.reply/d");

		if ($type == 1) {
			$order    = "scan asc, create_time desc";
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;

			$where = [];
			// 默认显示留言
			$where['type'] = 1;

			if (!empty($reply)) {
				$where['reply_id'] = $reply;
			}
			if (!empty($search)) {
				if ($search == 3) {
					$where['scan'] = -1;
				}else{
					$where['type'] = $search;
				}
			}

			// 统计
			$sum   = $message->get_count($where);
			// 获取数据
			$list   = $message->get_list_page($where, $order, $fields = "*", $start, $size);
			foreach ($list as $k => $v) {
				if ($v['type'] == 2) {
					$list[$k]['nickname'] = $user->get_field(['user_id' => $v['user_id']], "nickname");
					$list[$k]['isreply']  = -1;
				}else{
					// 是否回复
					$res   = $message->get_count(['reply_id' => $v['message_id']]);
					$list[$k]['isreply'] = ($res > 0)? 1: -1;

					// 用户名
					$list[$k]['nickname'] = $member->get_field(['user_id' => $v['user_id']], "nickname");
				}
			}

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $list;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('index');
	}
	
	// 删除
	public function delmessage()
	{
		$message = new MessageModel();
		$id      = input("param.message_id/d");
		$ids     = input("param.ids");

		if (!empty($id)) {
			$wherearr = [
				'type'     => 2,
				'reply_id' => $id,
			];
			$res = $message->get_count($wherearr);
			if ($res > 0) {
				echo json_encode(msg_error("该留言下有回复，不可删除"));
				exit;
			}

			$where = ['message_id' => $id];
		}

		if (!empty($ids)) {
			$ids = json_decode($ids);
			$wherearr = "reply_id IN (".implode(",", $ids).")";
			$res = $message->get_count($wherearr);
			if ($res > 0) {
				echo json_encode(msg_error("该留言下有回复，不可删除"));
				exit;
			}

			$where = "message_id IN (".implode(",", $ids).")";
		}

		// 删除数据
		$res = $message->del_data($where);
		if (isset($res['msg'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}

	// 查看留言
	public function read_message()
	{
		$message    = new MessageModel();
		$message_id = input("param.message_id/d");

		if (request()->isAjax()) {

			// 获取数据
			$cont = input("param.content");
			$id   = input("param.message_id/d");
			$data = [
				'message_id'  => $id,
				'content'   => $cont,
			];
			$res  = $message->save_data($data);
			if (isset($res['msg'])) {
				echo json_encode($res);exit;
			}

			echo json_encode(msg_success("编辑成功"));
			exit;
		}else{

			$where = ['message_id' => $message_id];
			$data  = $message->get_info($where);
			// 改变查看状态
			if ($data['scan'] == -1) {
				// 获取数据
				$condtion = [
					'message_id' => $message_id,
					'scan'       => 1,
				];
				$res  = $message->save_data($condtion);
				if (isset($res['msg'])) {
					echo $res['msg'];exit;
				}
			}

			return $this->fetch('edit', ['data' => $data]);
		}
	}


	// 修改留言状态
	public function change_status()
	{
		$message = new MessageModel();

		if (request()->isAjax()) {

			// 获取数据
			
			$status = input("param.status/d");
			$id     = input("param.id/d");
			$data   = [
				'message_id' => $id,
				'status'	 => $status,
			];

			$res  = $message->save_data($data);
			if (isset($res['msg'])) {
				echo json_encode($res);exit;
			}
			
			echo json_encode(msg_success("编辑成功"));
			exit;
		}
	}

	// 回复留言
	public function reply_message()
	{
		$message    = new MessageModel();
		$message_id = input("param.message_id/d");

		if (request()->isAjax()) {

			// 获取数据
			
			$cont = input("param.content");
			$id   = input("param.message_id/d");
			$data = [
				'reply_id'  => $id,
				'content'   => $cont,
				'type'      => 2,
				'user_ip'   => '127.0.0.1',
				'user_name' => 'root',
				'scan'      => 1,
				'status'    => 1
			];

			// user_id
			$user = session('user');
			$data['user_id'] = $user['user_id'];

			$res  = $message->insert_data($data);
			if (isset($res['msg'])) {
				echo json_encode($res);exit;
			}
			
			echo json_encode(msg_success("回复成功"));
			exit;
		}else{

			$data = ['message_id' => $message_id];
			return $this->fetch('reply', $data);

		}
	}	

	// 回复列表
	public function reply_list()
	{
		$message = new MessageModel();
		$user    = new UserModel();

		$message_id = input("param.message_id/d");
		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");

		if ($type == 1) {
			$order    = "create_time desc";
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;

			$where = [
				'type' => 2,
				'reply_id' => $message_id,
			];

			// 统计
			$sum   = $message->get_count($where);
			// 获取数据
			$list   = $message->get_list_page($where, $order, $fields = "*", $start, $size);
			foreach ($list as $k => $v) {
				$list[$k]['nickname'] = $user->get_field(['user_id' => $v['user_id']], "nickname");
			}
            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $list;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('list', ['message_id' => $message_id]);
	}
}


 ?>